import javax.swing.JFrame; //imports JFrame library
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton; //imports JButton library

import java.awt.*;

import javax.swing.*;

/*
 * NOTICE - THE RELEVANT NOTES ARE IN FULL CAPS - MAINLY WITHIN BUTTON_GRID() AND HIGHLIGHTCELL()
 */

public class ButtonGrid extends JPanel{
	public static final int SIZE = 10;
	JPanel frame = new JPanel(); //creates frame
	JButton[][] grid; //names the grid of buttons
	String clicked_button = "";
	
	public ButtonGrid() { //constructor
		frame.setLayout(new GridLayout(10, 10)); //set layout
		grid=new JButton[10][10]; //allocate the size of grid
		this.setPreferredSize(new Dimension(400,400));
		this.setMinimumSize(new Dimension(400,400));
		this.setMaximumSize(new Dimension(400,400));
		for(int x=0; x<10; x++){
			for(int y=0; y<10; y++){
				char c = (char) (x + 65);
				final int x_temp = x;
				final int y_temp = y;
				final char c_temp = c;
				grid[x][y]=new JButton(""+c+(y+1)); //creates new button    
				grid[x][y].setPreferredSize(new Dimension(50,50));
				grid[x][y].setMaximumSize(new Dimension(50,50));
				grid[x][y].setMinimumSize(new Dimension(50,50));
				
				/*
				 * THIS IS WHERE WE SET THE BACKGROUND OF EACH BUTTON IN OUR GRID -
				 * HOWEVER THE BUTTONS DEFAULT LOOK (ON MAC AT LEAST) REMAINS A BORDERED
				 * GRAY LOOK. 
				 */ 
				grid[x][y].setBackground(Color.gray);
				grid[x][y].setBorder(BorderFactory.createLineBorder(Color.black));
				grid[x][y].setOpaque(true);

				frame.add(""+c+(y+1), grid[x][y]); //adds button to grid
			}
		}
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//frame.pack(); //sets appropriate size for frame
		frame.setVisible(true); //makes frame visible
	}

	public void addActionListener (ActionListener click) {
		for (int x = 0; x < 10; x++) {
			for (int y = 0; y < 10; y++) {
				grid[x][y].addActionListener(click);
			}
		}
	}
	
	public void removeActionListener (ActionListener click) {
		for (int x = 0; x < 10; x++) {
			for (int y = 0; y < 10; y++) {
				grid[x][y].removeActionListener(click);
			}
		}
	}
	
	public void enableButtons () {
		for (int x = 0; x < 10; x++) {
			for (int y = 0; y < 10; y++) {
				grid[x][y].setEnabled(true);
			}
		}
	}
	
	public void disableButtons () {
		for (int x = 0; x < 10; x++) {
			for (int y = 0; y < 10; y++) {
				grid[x][y].setEnabled(false);
			}
		}
	}
	
	public String getClickedCell () {
		return this.clicked_button;
	}
	
	public void highlightCell (String cell) {
		/*
		 * HERE WE "HIGHLIGHT" THE POSSIBLE BUTTONS TO CLICK AND BECAUSE THERE IS NO REAL HIGHLIGHT OPTION
		 * WE SIMPLY ADDED A BORDER.
		 */
		int y = 0;
		int x = 0;

		if (cell.length() > 2 && cell.charAt(1) == '0') {
			x = Integer.valueOf(cell.substring(0,2)) - 1;
		}
		else {
			x = Character.getNumericValue(cell.charAt(0)) - 1;
		}
		if (cell.length() == 2) {
			y = Character.getNumericValue(cell.charAt(1)) - 1;
		}
		else {
			y = Integer.valueOf(cell.substring(1))-1;
		}
		System.out.println("Highlight: " + (x+1) +" "+ (y+1));
		grid[x][y].setBorder(BorderFactory.createLineBorder(Color.GREEN, 3));
		grid[x][y].setForeground(Color.green);
		grid[x][y].setOpaque(true);

	}
	
	public void dehighlightCell (String cell) {
		int y = 0;
		int x = 0;

		if (cell.length() > 2 && cell.charAt(1) == '0') {
			x = Integer.valueOf(cell.substring(0,2)) - 1;
		}
		else {
			x = Character.getNumericValue(cell.charAt(0)) - 1;
		}
		if (cell.length() == 2) {
			y = Character.getNumericValue(cell.charAt(1)) - 1;
		}
		else {
			y = Integer.valueOf(cell.substring(1))-1;
		}
		
		System.out.println("DeHighlight: " + (x+1) +" "+ (y+1));
		grid[x][y].setBackground(Color.gray);
		grid[x][y].setForeground(Color.black);
		grid[x][y].setBorder(BorderFactory.createLineBorder(Color.black));
		grid[x][y].setOpaque(true);
	}
	
	public void changeColor (String cell, String color) {
		int y = 0;
		int x = 0;

		if (cell.length() > 2 && cell.charAt(1) == '0') {
			x = Integer.valueOf(cell.substring(0,2)) - 1;
			System.out.println("X After Conversion: " + x);
		}
		else {
			x = Character.getNumericValue(cell.charAt(0)) - 10;
			System.out.println("X After Conversion: " + x);
		}
		if (cell.length() == 2) {
			y = Character.getNumericValue(cell.charAt(1)) - 1;
		}
		else {
			y = Integer.valueOf(cell.substring(1))-1;
		}
		
		System.out.println("Button Getting Changed: " + (x+1) + " " + (y+1));
		if (color.equals("RED")) {
			grid[x][y].setBorder(BorderFactory.createLineBorder(Color.RED, 3));
			grid[x][y].setForeground(Color.RED);
			grid[x][y].setOpaque(true);
		}
		else if (color.equals("YELLOW")) {
			grid[x][y].setBorder(BorderFactory.createLineBorder(Color.YELLOW, 3));
			grid[x][y].setForeground(Color.YELLOW);
			grid[x][y].setOpaque(true);		
		}
	}
}
